package com.example.demo.demos.web.test;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mess")
public class Messagewall {

    private volatile List<Mess> list = null;

    @ResponseBody
    @RequestMapping("/add")
    boolean add(@RequestBody Mess mess){
        System.out.println("add");
        if(!StringUtils.hasLength(mess.getFrom())||!StringUtils.hasLength(mess.getTo())
                ||!StringUtils.hasLength(mess.getSay())){
            return false;
        }
        if(list==null){
            synchronized (Messagewall.class) {
                if (list == null) {
                    list = new ArrayList<>();
                }
            }
        }
        list.add(mess);
        return true;
    }

    @ResponseBody
    @RequestMapping("/all")
    List<Mess> all(){
        return list;
    }
}
